package parser;

import java.util.HashMap;
import java.util.Map;

public class Parser {
	Map<String, String> variables;
	
	public Parser(){
		variables = new HashMap<String, String>();
	}
	
	public String getResult(String in){
		String[] tokens = in.split(" ");
		if(tokens.length != 3){
			return "invalid format";
		}
		if(variables.containsKey(tokens[0])){
			tokens[0] = variables.get(tokens[0]);
		}
		if(variables.containsKey(tokens[1])){
			tokens[1] = variables.get(tokens[1]);
		}
		if(variables.containsKey(tokens[2])){
			tokens[2] = variables.get(tokens[2]);
		}
		
		if(tokens[1].equals("=")){
			variables.put(tokens[0], tokens[2]);
			return "done";
		}
		try{
			double thing1 = Double.parseDouble(tokens[0]);
			double thing2 = Double.parseDouble(tokens[2]);
			if(tokens[1].equals("+")){
				return Double.toString(thing1 + thing2);
			}
			else if(tokens[1].equals("-")){
				return Double.toString(thing1 - thing2);
			}
			else if(tokens[1].equals("*")){
				return Double.toString(thing1 * thing2);
			}
			else if(tokens[1].equals("/")){
				return Double.toString(thing1 / thing2);
			}
			else if(tokens[1].equals("%")){
				return Double.toString(thing1 % thing2);
			}
			else{
				return "Operation not supported";
			}
		} catch (NumberFormatException e){
			return "Found something not a number when a number was expected";
		}
	}
	
	
	

}
